#!/bin/bash
device=$1
item=$2

case $item in
        speed)
            ethtool $device |grep Speed:|tr M " "|awk '{print $2}'
            ;;
	tx)
	    TXpre=$(cat /proc/net/dev | grep $device | tr : " " | awk '{print $10}')
	    sleep 1
	    TXnext=$(cat /proc/net/dev | grep $device | tr : " " | awk '{print $10}')
	    TX=$((${TXnext}-${TXpre}))
	    TX=$(($TX/(2**20)))
	    echo $TX
            ;;
	rx)
	    RXpre=$(cat /proc/net/dev | grep $device | tr : " " | awk '{print $2}')
	    sleep 1
	    RXnext=$(cat /proc/net/dev | grep $device | tr : " " | awk '{print $2}')
	    RX=$((${RXnext}-${RXpre}))
	    RX=$(($RX/(2**20)))
	    echo $RX 
            ;;
        per)
            SPEED=$(ethtool $device |grep Speed:|tr M " "|awk '{print $2}')
            TXpre=$(cat /proc/net/dev | grep $device | tr : " " | awk '{print $10}')
            RXpre=$(cat /proc/net/dev | grep $device | tr : " " | awk '{print $2}')
            sleep 1
            TXnext=$(cat /proc/net/dev | grep $device | tr : " " | awk '{print $10}')
            RXnext=$(cat /proc/net/dev | grep $device | tr : " " | awk '{print $2}')
            RX=$((${RXnext}-${RXpre}))
            TX=$((${TXnext}-${TXpre}))
            RX=$((${TX}+${RX}))
            RX=$(($RX/(2**20)))
            awk 'BEGIN{printf "%.2f\n",('$RX'/'$SPEED')*100}'
            ;;
	onlien)
	    ip link|grep -i " $device[':''@']"|awk -F 'state' '{print $2}'|awk '{print $1}'
	    ;;
esac

